@{
    Validation.Exclude("content","summary");
    // ADD AN ARTICLE
    
    // DEFINE VIEW ;
    var view = "~/Views/Admin/_AddArticleView.cshtml";
    // DEFINE VARS
    string title ,summary ,content,published,id,categoryId,enableComments,showComments;
 
    // GET CATEGORY list
    var Categories = Blog.DataObject.GetCategories();
    // GET USER ID
    
    var authorId = WebSecurity.CurrentUserId;
    
    // IF POST CHECK FORM
    if(IsPost){
        // GET POST VARIABLES
        title = Request.Form["title"];
        summary = Request.Form["summary"];
        content = Request.Form["content"];
        published = Request.Form["published"];
        categoryId = Request.Form["categoryId"];
        enableComments = (String.IsNullOrEmpty(Request.Form["enableComments"]))?"0":Request.Form["enableComments"];
        showComments = (String.IsNullOrEmpty(Request.Form["showComments"]))?"0":Request.Form["showComments"];
        
        // VALIDATE FORM
        
        if(title.IsEmpty()){ ModelState.AddFormError("Title cannot be empty");}
        if(summary.IsEmpty() && content.IsEmpty()){ModelState.AddError("content","Provide at least a summary or a content");} 
        if(!enableComments.IsInt() && !String.IsNullOrEmpty(enableComments)){ModelState.AddFormError("enable comments invalid");};
        if(!showComments.IsInt()&& !String.IsNullOrEmpty(showComments) ){ModelState.AddFormError("show comments invalid");};
        if(!categoryId.IsInt() || String.IsNullOrEmpty(categoryId) ){ModelState.AddFormError("You must choose a category");};
        if(String.IsNullOrEmpty(published)){published="0";}
        // IF FORM VALID
        if(ModelState.IsValid){
            // form is valid , try to insert an article in the database
            var query = Blog.DataObject.AddArticle(title,summary,content,categoryId,authorId.ToString(),published,enableComments,showComments);
            // set the return view
            view = "~/Actions/Admin/ListAllArticles";
            if(query>0){
                // SUCCESS
                Response.Redirect(@Href(view,new{successMessage="New article added"}));
            }else{
                // FAILURE
                Response.Redirect(@Href(view,new{errorMessage="No article added"}));
            }
            
        }
    }
    Page.Errors = Html.ValidationSummary() ;    
}
@*ObjectInfo.Print(Request.Form)*@
@RenderPage(view,new{categories = Categories})
